<?php

namespace App\Http\Controllers\Auth;

use App\Http\Common\MyUtils;
use App\Http\Common\WeChatApp;
use App\Http\Controllers\BaseController;
use EasyWeChat\Factory;

/**
 * 微信端登录
 * Class WechatLoginController
 * @package App\Http\Controllers\Auth
 */
class WechatLoginController extends BaseController
{
    /**
     * 拿到微信登录的二维码
     *data:
     {
        sceneId: "processon_e1a74a11-8c68-4a0f-b6c4-3371eba19291",
        QRcodeUrl: "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQF58DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyTkZMWThIMjZib1QxX1haU050Y2UAAgTr7-ZdAwQQDgAA"
        sceneId: "processon_e1a74a11-8c68-4a0f-b6c4-3371eba19291"
        result: "success"
      }
     * @return string
     */
    public function getQrCode()
    {
        $instance = WeChatApp::getPublicInstance();
        $instance->setConfig(0,0,0,0,true);
        $app = Factory::officialAccount($instance->config);
        $qrcode = $app->qrcode;
        $result = $qrcode->temporary(56, 6 * 24 * 3600);
        $ticket = "TICKET";
        $url = $result->url($ticket); // 二维码图片解析后的地址，开发者可根据该地址自行生成需要的二维码图片
        $data['QRcodeUrl'] = $url;
        return MyUtils::dealFD($data,1);
    }
    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        $wechat = app('wechat');
        $wechat->server->setMessageHandler(function($message){
            return "欢迎关注 物业后台管理系统！";
        });
        $this->add_menu();
        return $wechat->server->serve();
    }

    public function add_menu(){
        $app = app('wechat');
        $menu = $app->menu;
        $buttons = [
            [
                "type"=>"view",
                "name"=>"进入系统",
                "url"=>"http://vote.mrwangqi.com/user",
            ],
        ];
        $menu->add($buttons);
    }
}
